<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Model\Main;
use App\Model\MainGallery;
use App\Http\Requests\Main\UpdateMainRequest;

use Illuminate\Http\Request;
use JWTAuth;
class MainController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_main'=>'am-active',
        ]);
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        
        $main=Main::with('main_galleries')->first();

        return view('Admin.xHotel.main.index')->with('main',$main);
    }

    /**
     * 修改显示页
     * @param $id
     */
    public function edit($id)
    {
        $main = Main::find($id);

        if(!$main){

            abort(404, 'Unauthorized action.');
        }
        return view('Admin.xHotel.main.edit')->with('main',$main);
    }


    /**
     * 修改
     * @param UpdateMainRequest $request
     * @param $id
     */
    public function update(UpdateMainRequest $request, $id)
    {

        //return $request->all();
        $data = $request->except(['file', 'imgs']);
        
        $main = Main::find($id);

        $main->update($data);

        //相册
        if ($request->has('imgs')) {
            foreach ($request->imgs as $img) {
                $main->main_galleries()->create(['img' => $img]);
            }
        }

        return redirect('/main')->with('success', '编辑成功');
    }

    /**
     * ajax删除相册图片
     * @param Request $request
     */
    public function destroy_gallery(Request $request){
        MainGallery::destroy($request->gallery_id);
    }

    
}
